home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / human interface toolbox / packagetool / sample package / htmlsample sources / ciconbuttons.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  6.3 KB  |  168 lines

  1. /*
  2.     File: CIconButtons.c
  3.     
  4.     Description:
  5.         This file contains routines used to implement the color icon buttons
  6.     displayed in the top of HTMLSample's windows.
  7.     
  8.     HTMLSample is an application illustrating how to use the new
  9.     HTMLRenderingLib services found in Mac OS 9. HTMLRenderingLib
  10.     is Apple's light-weight HTML rendering engine capable of
  11.     displaying HTML files.
  12.  
  13.  
  14.     Copyright:
  15.         © Copyright 1999 Apple Computer, Inc. All rights reserved.
  16.     
  17.     Disclaimer:
  18.         IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  19.         ("Apple") in consideration of your agreement to the following terms, and your
  20.         use, installation, modification or redistribution of this Apple software
  21.         constitutes acceptance of these terms.  If you do not agree with these terms,
  22.         please do not use, install, modify or redistribute this Apple software.
  23.  
  24.         In consideration of your agreement to abide by the following terms, and subject
  25.         to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  26.         copyrights in this original Apple software (the "Apple Software"), to use,
  27.         reproduce, modify and redistribute the Apple Software, with or without
  28.         modifications, in source and/or binary forms; provided that if you redistribute
  29.         the Apple Software in its entirety and without modifications, you must retain
  30.         this notice and the following text and disclaimers in all such redistributions of
  31.         the Apple Software.  Neither the name, trademarks, service marks or logos of
  32.         Apple Computer, Inc. may be used to endorse or promote products derived from the
  33.         Apple Software without specific prior written permission from Apple.  Except as
  34.         expressly stated in this notice, no other rights or licenses, express or implied,
  35.         are granted by Apple herein, including but not limited to any patent rights that
  36.         may be infringed by your derivative works or by other works in which the Apple
  37.         Software may be incorporated.
  38.  
  39.         The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  40.         WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  41.         WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42.         PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  43.         COMBINATION WITH YOUR PRODUCTS.
  44.  
  45.         IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  46.         CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  47.         GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48.         ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  49.         OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  50.         (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  51.         ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  52.  
  53.     Change History (most recent first):
  54.         Wed, Dec 22, 1999 -- created
  55. */
  56.  
  57.  
  58. #include "CIconButtons.h"
  59. #include <QuickDraw.h>
  60. #include <Icons.h>
  61. #include <Events.h>
  62. #include <Resources.h>
  63. #include <string.h>
  64.  
  65.  
  66.  
  67. /* NewCIconButton retrieves a new color icon button
  68.     resource from the resource file.  the id number
  69.     corresponds to the resource id of the CICB resource. */
  70. CIconButtonHandle NewCIconButton(short id) {
  71.     Handle theRsrc;
  72.     theRsrc = GetResource(kIconButtonType, id);
  73.         /* here, we call detach resource because we
  74.         may have many windows open with many buttons
  75.         each having a different state. */
  76.     if (theRsrc != NULL) DetachResource(theRsrc);
  77.     return (CIconButtonHandle) theRsrc;
  78. }
  79.  
  80.  
  81. /* DisposeCIconButton disposes of any structures allocated
  82.     for the color icon button allocated by NewCIconButton. */
  83. void DisposeCIconButton(CIconButtonHandle cicb) {
  84.     DisposeHandle((Handle) cicb);
  85. }
  86.  
  87.  
  88.  
  89. /* GetCIconButtonStringData returns a new handled containing
  90.     the string data copied from the color icon resource.  The
  91.     handle will contain a C-style string terminated with a zero
  92.     byte.  It is the caller's responsibility to dispose of this
  93.     handle after it has been used. */
  94. OSErr GetCIconButtonStringData(CIconButtonHandle cicb, Handle *strdata) {
  95.     char handstate;
  96.     OSErr err;
  97.     handstate = HGetState((Handle) cicb);
  98.     HLock((Handle) cicb);
  99.     err = PtrToHand((**cicb).stringdata, strdata, strlen((**cicb).stringdata) + 1);
  100.     HSetState((Handle) cicb, handstate);
  101.     return err;
  102. }
  103.  
  104.  
  105. /* SetCIconButtonPosition sets the color icon button's 
  106.     screen postion. h and v are coordinates in the
  107.     current grafport. */
  108. void SetCIconButtonPosition(CIconButtonHandle cicb, short h, short v) {
  109.     char handstate;
  110.     handstate = HGetState((Handle) cicb);
  111.     HLock((Handle) cicb);
  112.     OffsetRect(&(**cicb).bounds, h - (**cicb).bounds.left, v - (**cicb).bounds.top);
  113.     HSetState((Handle) cicb, handstate);
  114. }
  115.  
  116.  
  117. /* DrawCIconButton draws the icon button using the
  118.     as it should appear given the state specified.  state
  119.     should be either kCBdisabled, kCBup, or kCBdown.
  120.     The last state a button is drawn in affects the
  121.     result of TrackCIconButton. */
  122. void DrawCIconButton(CIconButtonHandle cicb, short state) {
  123.     CIconHandle theIcon;
  124.     char handstate;
  125.     handstate = HGetState((Handle) cicb);
  126.     HLock((Handle) cicb);
  127.     theIcon = GetCIcon((**cicb).cicnIDs[state]);
  128.     if (theIcon != NULL) {
  129.         PlotCIcon(&(**cicb).bounds, theIcon);
  130.         DisposeCIcon(theIcon);
  131.     }
  132.     (**cicb).drawnstate = state;
  133.     HSetState((Handle) cicb, handstate);
  134. }
  135.  
  136.  
  137. /* TrackCIconButton should be called whenever a click is made
  138.     inside of a color icon button.  if the last time the button
  139.     was drawn its state was not kCBup or the click is outside
  140.     of the button, then this routine returns false. */
  141. Boolean TrackCIconButton(CIconButtonHandle cicb, Point where) {
  142.     Boolean result;
  143.     char handstate;
  144.     handstate = HGetState((Handle) cicb);
  145.     HLock((Handle) cicb);
  146.     if ((**cicb).drawnstate == kCBdisabled) {
  147.         result = false;
  148.     } else if (PtInRect(where, &(**cicb).bounds)) {
  149.         Boolean isin, ishilited;
  150.         Point mouseLoc;
  151.         isin = true;
  152.         ishilited = true;
  153.         mouseLoc = where;
  154.         DrawCIconButton(cicb, kCBdown);
  155.         while (StillDown()) {
  156.             GetMouse(&mouseLoc);
  157.             isin = PtInRect(mouseLoc, &(**cicb).bounds);
  158.             if (isin != ishilited) {
  159.                 ishilited = isin;
  160.                 DrawCIconButton(cicb, ishilited ? kCBdown : kCBup);
  161.             }
  162.         }
  163.         result = ishilited;
  164.     } else result = false;
  165.     HSetState((Handle) cicb, handstate);
  166.     return result;
  167. }
  168.